$(function(){
    obj.init();
});

window.obj = new JsFun();
function JsFun() {
    'use strict';
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#List'), 'Schoolgroup/getGroup');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del'], ['bt_list_user', 'bt_list_school']);

            //绑定删除
            xhExtra.bindListDel(this.dataGrid, 'iSGId', 'Schoolgroup/delGroup');
            xhTool.bindDocKeyDown($('#bt_list_search'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            //增加组
            $('#bt_list_add').click(function () {
                $this.funObj().addGroup();
            });
            //搜索组
            $('#bt_list_search').click(function () {
                $this.funObj().searchGroup();
            });
            //关联用户
            $('#bt_list_user').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().contactUser();
            });
            //关联学校
            $('#bt_list_school').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().contactSchool();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                addGroup: function () {
                    window.top.swal({
                        title: '添加学校组',
                        confirmButtonText: '增加',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        html: '<input id="groupName" class="swal2-input" placeholder="学校组名称" />' +
                            '<select id="groupType" class="swal2-select" style="width: 100%;"><option value="1">查看组</option><option value="2">管理组</option></select>' +
                            '<textarea id="remark" class="swal2-textarea" placeholder="备注" rows="2"></textarea>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var groupName   = $('#groupName', window.top.document).val(),
                                    groupType   = $('#groupType', window.top.document).val(),
                                    remark      = $('#remark', window.top.document).val();
                                if (!groupName){
                                    reject('组名不正确！');
                                }
                                else {
                                    xhExtra.ajaxRequest('Schoolgroup/createGroup',
                                        {groupName: groupName, groupType: groupType, remark: remark},
                                        {success: true, table: $this.dataGrid}
                                    );
                                }
                            })
                        }
                    });
                },
                searchGroup: function () {
                    $this.dataGrid.reload({
                        search : {
                            vague: xhExtra.getValue($('#list_vague'))
                        }
                    });
                },
                contactUser: function () {
                    var groupId = $this.dataGrid.getSelectValue('iSGId');
                    if (!groupId) {
                        window.top.xhTop.showError('选中id有误！');
                    }
                    window.top.xhTop.showModal({
                        title: '用户关联-' + $this.dataGrid.getSelectValue('sGroupName'),
                        content: xhTool.getFrameString('Schoolgroup/groupUser', {groupId: groupId}, 550),
                        size: 'lg',
                        button: true,
                        backClose: false
                    });
                },
                contactSchool: function () {
                    var groupId = $this.dataGrid.getSelectValue('iSGId');
                    if (!groupId) {
                        window.top.xhTop.showError('选中id有误！');
                    }
                    window.top.xhTop.showModal({
                        title: '学校关联-' + $this.dataGrid.getSelectValue('sGroupName'),
                        content: xhTool.getFrameString('Schoolgroup/groupSchool', {groupId: groupId}, 550),
                        size: 'lg',
                        button: true,
                        backClose: false
                    });
                }
            }
        }
    }
}